using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExpertSMS.Models;

namespace ExpertSMS.Controllers
{   
    public class FoodProductsController : Controller
    {
        private ExpertSMSContext context = new ExpertSMSContext();

        public ActionResult _FoodProductDetails(int foodID)
        {
            ViewBag.PossibleProducts = context.Products;
            return PartialView(foodID);
        }


        [HttpPost]
        public ActionResult _FoodProductDetails(int productID,int? unitID, decimal? quantity, int foodID)
        {
            Product product = context.Products.Single(x => x.ProductID == productID);
            ViewBag.PossibleProducts = product;
            IEnumerable<Unit> units = product.Units;
            ViewBag.PossibleUnits = units;
            if (unitID.HasValue)
            {
                ViewBag.PossibleUnit = units.FirstOrDefault(x=>x.UnitID == unitID.Value);
            }

            return PartialView(foodID);
        }
        [HttpPost]
        public ActionResult _FoodProductList(int foodID)
        {
            return PartialView(context.Foods.Single(x => x.FoodID == foodID).FoodProducts);
        }

        public ViewResult Index()
        {
            return View(context.FoodProducts.Include(foodproduct => foodproduct.Product).Include(foodproduct => foodproduct.Food).Include(foodproduct => foodproduct.Unit).ToList());
        }

        //
        // GET: /FoodProducts/Details/5

        public ViewResult Details(int id)
        {
            FoodProduct foodproduct = context.FoodProducts.Single(x => x.FoodProductID == id);
            return View(foodproduct);
        }

        //
        // GET: /FoodProducts/Create

        public ActionResult Create()
        {
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleFoods = context.Foods;
            ViewBag.PossibleUnits = context.Units;
            return View();
        } 

        //
        // POST: /FoodProducts/Create

        [HttpPost]
        public ActionResult Create(FoodProduct foodproduct)
        {
            if (ModelState.IsValid)
            {
                context.FoodProducts.Add(foodproduct);
                context.SaveChanges();
                context = new ExpertSMSContext();
                return PartialView("_FoodProductList", context.Foods.Single(x => x.FoodID == foodproduct.FoodID).FoodProducts);  
            }

            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleFoods = context.Foods;
            ViewBag.PossibleUnits = context.Units;
            return View(foodproduct);
        }
        
        //
        // GET: /FoodProducts/Edit/5
 
        public ActionResult Edit(int id)
        {
            FoodProduct foodproduct = context.FoodProducts.Single(x => x.FoodProductID == id);
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleFoods = context.Foods;
            ViewBag.PossibleUnits = context.Units;
            return View(foodproduct);
        }

        //
        // POST: /FoodProducts/Edit/5

        [HttpPost]
        public ActionResult Edit(FoodProduct foodproduct)
        {
            if (ModelState.IsValid)
            {
                context.Entry(foodproduct).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleFoods = context.Foods;
            ViewBag.PossibleUnits = context.Units;
            return View(foodproduct);
        }

        //
        // GET: /FoodProducts/Delete/5
 
        public ActionResult Delete(int id)
        {
            FoodProduct foodproduct = context.FoodProducts.Single(x => x.FoodProductID == id);
            return View(foodproduct);
        }

        //
        // POST: /FoodProducts/Delete/5

        public ActionResult DeleteConfirmed(int id)
        {
            FoodProduct foodproduct = context.FoodProducts.Single(x => x.FoodProductID == id);
            context.FoodProducts.Remove(foodproduct);
            context.SaveChanges();
            context = new ExpertSMSContext();
            return PartialView("_FoodProductList", context.Foods.Single(x => x.FoodID == foodproduct.FoodID).FoodProducts);  

        }
    }
}